<template>
  <view class="login-container">
    <view class="login-header">
      <image class="logo" src="/static/logo.svg" mode="aspectFit" />
      <text class="app-title">VKG AI</text>
      <text class="app-subtitle">智能对话助手</text>
    </view>

    <view class="login-form">
      <view class="form-tabs">
        <button 
          :class="['tab-btn', activeTab === 'login' ? 'active' : '']" 
          @tap="activeTab = 'login'"
        >
          登录
        </button>
        <button 
          :class="['tab-btn', activeTab === 'register' ? 'active' : '']" 
          @tap="activeTab = 'register'"
        >
          注册
        </button>
      </view>

      <!-- 登录表单 -->
      <view v-if="activeTab === 'login'" class="form-content">
        <view class="form-item">
          <text class="label">用户名</text>
          <input 
            v-model="loginForm.username" 
            class="input" 
            placeholder="请输入用户名"
            type="text"
          />
        </view>

        <view class="form-item">
          <text class="label">密码</text>
          <input 
            v-model="loginForm.password" 
            class="input" 
            placeholder="请输入密码"
            type="password"
            password
          />
        </view>

        <button class="submit-btn" @tap="handleLogin" :disabled="isLoading">
          <text v-if="!isLoading">登录</text>
          <text v-else>登录中...</text>
        </button>

        <button class="guest-btn-below" @tap="guestLogin">
          <text class="icon">👤</text>
          <text>游客登录</text>
        </button>
      </view>

      <!-- 注册表单 -->
      <view v-if="activeTab === 'register'" class="form-content">
        <view class="form-item">
          <text class="label">用户名</text>
          <input 
            v-model="registerForm.username" 
            class="input" 
            placeholder="请输入用户名"
            type="text"
          />
        </view>

        <view class="form-item">
          <text class="label">邮箱</text>
          <input 
            v-model="registerForm.email" 
            class="input" 
            placeholder="请输入邮箱"
            type="email"
          />
        </view>

        <view class="form-item">
          <text class="label">密码</text>
          <input 
            v-model="registerForm.password" 
            class="input" 
            placeholder="请输入密码"
            type="password"
            password
          />
        </view>

        <view class="form-item">
          <text class="label">确认密码</text>
          <input 
            v-model="registerForm.confirmPassword" 
            class="input" 
            placeholder="请再次输入密码"
            type="password"
            password
          />
        </view>

        <button class="submit-btn" @tap="handleRegister" :disabled="isLoading">
          <text v-if="!isLoading">注册</text>
          <text v-else>注册中...</text>
        </button>
      </view>
    </view>

  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'login',
      isLoading: false,
      loginForm: {
        username: '',
        password: ''
      },
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  methods: {
    async handleLogin() {
      if (!this.validateLoginForm()) return
      
      this.isLoading = true
      try {
        // 模拟登录请求
        await this.simulateLogin()
        
        // 保存登录状态
        uni.setStorageSync('token', 'mock-token-' + Date.now())
        uni.setStorageSync('userInfo', {
          username: this.loginForm.username,
          avatar: '/static/logo.svg'
        })
        
        uni.showToast({
          title: '登录成功',
          icon: 'success'
        })
        
        // 跳转到VKG-ai页面
        setTimeout(() => {
          uni.navigateTo({
            url: '/pages/vkg-ai/index'
          })
        }, 1500)
        
      } catch (error) {
        uni.showToast({
          title: '登录失败，请重试',
          icon: 'none'
        })
      } finally {
        this.isLoading = false
      }
    },

    async handleRegister() {
      if (!this.validateRegisterForm()) return
      
      this.isLoading = true
      try {
        // 模拟注册请求
        await this.simulateRegister()
        
        uni.showToast({
          title: '注册成功',
          icon: 'success'
        })
        
        // 切换到登录标签
        this.activeTab = 'login'
        this.clearForm()
        
      } catch (error) {
        uni.showToast({
          title: '注册失败，请重试',
          icon: 'none'
        })
      } finally {
        this.isLoading = false
      }
    },

    validateLoginForm() {
      if (!this.loginForm.username.trim()) {
        uni.showToast({
          title: '请输入用户名',
          icon: 'none'
        })
        return false
      }
      if (!this.loginForm.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return false
      }
      return true
    },

    validateRegisterForm() {
      if (!this.registerForm.username.trim()) {
        uni.showToast({
          title: '请输入用户名',
          icon: 'none'
        })
        return false
      }
      if (!this.registerForm.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return false
      }
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        })
        return false
      }
      return true
    },

    clearForm() {
      this.loginForm = {
        username: '',
        password: ''
      }
      this.registerForm = {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      }
    },

    simulateLogin() {
      return new Promise((resolve) => {
        setTimeout(() => {
          // 模拟登录成功
          resolve()
        }, 1000)
      })
    },

    simulateRegister() {
      return new Promise((resolve) => {
        setTimeout(() => {
          // 模拟注册成功
          resolve()
        }, 1000)
      })
    },

    guestLogin() {
      uni.showLoading({
        title: '登录中...'
      })
      
      setTimeout(() => {
        uni.hideLoading()
        
        // 模拟游客登录
        const guestUser = {
          id: 'guest_' + Date.now(),
          username: '游客' + Math.floor(Math.random() * 1000),
          email: '',
          isGuest: true
        }
        
        uni.setStorageSync('token', 'guest_token_' + Date.now())
        uni.setStorageSync('userInfo', guestUser)
        
        uni.showToast({
          title: '游客登录成功',
          icon: 'success'
        })
        
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/vkg-ai/index'
          })
        }, 1500)
      }, 1000)
    }
  },

  onLoad() {
    // 检查是否已登录
    const token = uni.getStorageSync('token')
    if (token) {
      uni.navigateTo({
        url: '/pages/vkg-ai/index'
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  min-height: 100vh;
  height: auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 10rpx 40rpx 120rpx;
  box-sizing: border-box;
  overflow-y: auto;
}

.login-header {
    text-align: center;
    margin-bottom: 30rpx;
  }

.logo {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 30rpx;
}

.app-title {
  display: block;
  font-size: 48rpx;
  font-weight: bold;
  color: white;
  margin-bottom: 20rpx;
}

.app-subtitle {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.8);
}

.login-form {
  background: white;
  border-radius: 20rpx;
  padding: 50rpx 50rpx 70rpx;
  width: 100%;
  max-width: 600rpx;
  box-shadow: 0 15rpx 30rpx rgba(0, 0, 0, 0.08);
  margin: 10rpx 0 60rpx;
  min-height: auto;
}

.form-tabs {
    display: flex;
    margin-bottom: 25rpx;
    border-bottom: 2rpx solid #eee;
  }

.tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  font-size: 32rpx;
  color: #999;
  padding: 20rpx 0;
  position: relative;
}

.tab-btn.active {
  color: #667eea;
  font-weight: bold;
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: -2rpx;
  left: 0;
  right: 0;
  height: 4rpx;
  background: #667eea;
}

.form-content {
  width: 100%;
}

.form-item {
  margin-bottom: 50rpx;
}

.label {
    display: block;
    font-size: 26rpx;
    color: #333;
    margin-bottom: 8rpx;
    font-weight: 500;
  }

.input {
  width: 100%;
  height: 75rpx;
  background: #f8f9fa;
  border: 2rpx solid #e9ecef;
  border-radius: 38rpx;
  padding: 0 25rpx;
  font-size: 28rpx;
  color: #333;
  box-sizing: border-box;
}

.input:focus {
  border-color: #667eea;
  outline: none;
}



.submit-btn {
    width: 100%;
    height: 80rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: 2rpx solid transparent;
    border-radius: 40rpx;
    font-size: 32rpx;
    font-weight: 600;
    margin-top: 40rpx;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 6rpx 16rpx rgba(102, 126, 234, 0.3);
  }

.submit-btn:active {
  background: #5a6fd8;
}

.submit-btn:disabled {
  background: #b3b3b3;
  cursor: not-allowed;
}

.guest-btn-below {
    width: 100%;
    height: 80rpx;
    background: rgba(102, 126, 234, 0.05);
    color: #667eea;
    border: 2rpx solid #667eea;
    border-radius: 40rpx;
    font-size: 32rpx;
    font-weight: 600;
    margin-top: 30rpx;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12rpx;
    box-shadow: 0 3rpx 10rpx rgba(102, 126, 234, 0.15);
  }

  .guest-btn-below:active {
    background: rgba(102, 126, 234, 0.1);
  }

  .guest-btn-below .icon {
    font-size: 35rpx;
  }

/* 响应式适配 */
@media screen and (max-width: 768px) {
  .login-container {
    padding: 20rpx;
    height: 100vh;
    overflow: hidden;
  }
  
  .login-form {
    padding: 60rpx 40rpx;
    max-width: 100%;
    margin: 0 auto;
  }
  
  .login-header {
    margin-bottom: 60rpx;
  }
  
  .logo {
    width: 100rpx;
    height: 100rpx;
    margin-bottom: 20rpx;
  }
  
  .app-title {
    font-size: 42rpx;
  }
  
  .app-subtitle {
    font-size: 26rpx;
  }
}

@media screen and (max-width: 480px) {
  .login-container {
    padding: 0;
  justify-content: flex-start;
  padding-top: 20rpx;
  }
  
  .login-form {
    border-radius: 0;
    padding: 50rpx 35rpx;
    min-height: auto;
  }
  
  .form-tabs {
    margin-bottom: 40rpx;
  }
  
  .form-item {
    margin-bottom: 20rpx;
  }
  
  .input {
    height: 85rpx;
    font-size: 28rpx;
  }
  
  .submit-btn,
  .guest-btn-below {
    height: 85rpx;
    font-size: 30rpx;
  }
}

/* 微信小程序不需要去除滚动条，页面默认无滚动条 */
page {
  overflow-x: hidden;
  overflow-y: hidden;
}



/* 超小屏幕适配 */
@media screen and (max-width: 375px) {
  .login-container {
    padding: 0;
  justify-content: flex-start;
  padding-top: 10rpx;
  min-height: 100vh;
  }
  
  .login-form {
    border-radius: 0;
    padding: 40rpx 30rpx;
    width: 100%;
    max-width: 100%;
    min-height: auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
  }
  
  .login-header {
    margin-bottom: 40rpx;
  }
  
  .logo {
    width: 80rpx;
    height: 80rpx;
    margin-bottom: 15rpx;
  }
  
  .app-title {
    font-size: 36rpx;
    margin-bottom: 15rpx;
  }
  
  .app-subtitle {
    font-size: 24rpx;
  }
  
  .form-tabs {
    margin-bottom: 30rpx;
  }
  
  .tab-btn {
    font-size: 28rpx;
    padding: 15rpx 0;
  }
  
  .form-item {
    margin-bottom: 18rpx;
  }

  .label {
    font-size: 24rpx;
    margin-bottom: 6rpx;
  }

  .input {
    height: 70rpx;
    font-size: 26rpx;
    padding: 0 20rpx;
  }

  .password-input .input {
    padding-right: 70rpx;
  }

  .eye-btn {
    font-size: 26rpx;
    right: 18rpx;
    width: 35rpx;
    height: 35rpx;
    background: transparent;
    border: none;
    outline: none;
    box-shadow: none;
    margin: 0;
    padding: 0;
    -webkit-appearance: none;
    appearance: none;
  }

  .submit-btn {
    height: 70rpx;
    font-size: 26rpx;
    margin-top: 25rpx;
  }
  
  .guest-btn-below {
    height: 80rpx;
    font-size: 28rpx;
    margin-top: 20rpx;
  }
}

/* 大屏幕优化 */
@media screen and (min-width: 769px) {
  .login-form {
      max-width: 650rpx;
    }
}

/* 确保内容不溢出 */
view, text, input, button {
  max-width: 100%;
  box-sizing: border-box;
}

/* 防止软键盘遮挡 */
@media screen and (max-height: 600px) {
  .login-container {
    justify-content: flex-start;
    padding-top: 40rpx;
  }
  
  .login-header {
    margin-bottom: 30rpx;
  }
  
  .logo {
    width: 60rpx;
    height: 60rpx;
    margin-bottom: 10rpx;
  }
  
  .app-title {
    font-size: 32rpx;
  }
  
  .app-subtitle {
    font-size: 22rpx;
  }
}
</style>